# -*- coding:utf-8 -*-
# @FileName  :send_message.py
# @Time      :2021-01-30 15:59
# @Author    :niuhanyang

import yamail
import requests
import time
import hmac
import hashlib
import base64
import urllib.parse

password="ogcdbxrrbfiybeic"
host="smtp.qq.com"
user="925182968@qq.com"
to = ["wangxiaoyan60@jd.com"]
cc = ["511402865@qq.com","925182968@qq.com"]
url = "https://oapi.dingtalk.com/robot/send?access_token=ea67b3129ba5b6d54b186af690023b0290800075d6dfa46caf8eca68fca5ccb7"



def send_mail(subject,content,files=None): #发邮件函数
    smtp = yamail.SMTP(host=host,
                       user=user,
                       password=password,
                       )
    smtp.send(to=to, cc=cc, subject=subject, contents=content, attachments=files)


#在叮叮群管理里面点击添加群机器人，设置机器人名字，选择安全设置后，会生成接口地址
def get_sign():  #发送叮叮群信息加签函数
    timestamp = str(round(time.time() * 1000))
    secret = 'SEC12e01f60fb742c3e63b1f45b636bf3246cb566a521dc9d70f70ba9e8d57261c9'
    secret_enc = secret.encode()
    string_to_sign = timestamp + '\n' + secret
    string_to_sign_enc = string_to_sign.encode()
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    return timestamp,sign

def send_dd(content,at=None,at_all=False): #叮叮机器人发送叮叮群消息函数
    data = {
        "msgtype": "text",
        "text": {
            "content": content
        },
        "at": {
            "atMobiles": at,
            "isAtAll": at_all
        }
    }
    timestamp, sign = get_sign()
    params = {"timestamp": timestamp, 'sign': sign}
    r = requests.post(url, json=data, params=params)
    return r.json()
